#include "textures.h"
#include <cmath>
#include <string>
#include <windows.h>
#include <GL/glut.h>
#include <iostream> //to output
float scale = 1;

// *********************************

 Textures::Textures(){
	glGenTextures(1, &texture_id); //we need to create an unique ID for the texture
}

 Textures::~Textures(){

}

//void Textures::LoadTGA(const char* filename)
//TGAInfo* LoadTGA(const char* filename)
bool Textures::LoadTGA(const char* filename)
{
	
    GLubyte TGAheader[12] = {0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    GLubyte TGAcompare[12];
    GLubyte header[6];
    GLuint bytesPerPixel;
    GLuint imageSize;
    GLuint temp;
    GLuint type = GL_RGBA;

	char * a;
	a = (char*)malloc(4*sizeof(char));
	int len = strlen(filename);
	sprintf(a,"%c%c%c%c", filename[len-4],filename[len-3],filename[len-2],filename[len-1]);
	std::cout << "LoadTGA Var a = " << a << std::endl;
	char* path = (char*) malloc ((13+strlen(filename))*sizeof(char));
	
	if( strcmp(".tga",a) == 0)
		sprintf(path,"textures/%s",filename);
	else
		sprintf(path,"textures/%s.tga",filename);
    
	std::cout << "LoadTGA File to open = " << path << std::endl;
	FILE * file = fopen(path, "rb");
    
    if (file == NULL || fread(TGAcompare, 1, sizeof(TGAcompare), file) != sizeof(TGAcompare) ||
        memcmp(TGAheader, TGAcompare, sizeof(TGAheader)) != 0 ||
        fread(header, 1, sizeof(header), file) != sizeof(header))
    {
        if (file == NULL)
            //return NULL;
			return false;
        else
        {
            fclose(file);
            //return NULL;
			return false;
        }
    }
	std::cout << "file readed" << std::endl;
	TGAInfo* tgainfo = new TGAInfo;
    
    tgainfo->width = header[1] * 256 + header[0];
    tgainfo->height = header[3] * 256 + header[2];
    
    if (tgainfo->width <= 0 || tgainfo->height <= 0 || (header[4] != 24 && header[4] != 32))
    {
        fclose(file);
		delete tgainfo;
        //return NULL;
		return false;
    }
    
    tgainfo->bpp = header[4];
    bytesPerPixel = tgainfo->bpp / 8;
    imageSize = tgainfo->width * tgainfo->height * bytesPerPixel;
    
    tgainfo->data = (GLubyte*)malloc(imageSize);

    if (tgainfo->data == NULL ||  (fread( tgainfo->data, 1, imageSize, file)  != imageSize))
    {
        if (tgainfo->data != NULL)
            free(tgainfo->data);
            
        fclose(file);
		delete tgainfo;
        //return NULL;
		return false;
    }
	std::cout << "LoadTGA :: data readed" << std::endl;
    for (GLuint i = 0; i < int(imageSize); i += bytesPerPixel)
    {
        temp = tgainfo->data[i];
        tgainfo->data[i] = tgainfo->data[i + 2];
        tgainfo->data[i + 2] = temp;
    }
    
    fclose(file);

	//return tgainfo;
	std::cout << "LoadTGA :: Binding Texture" << std::endl;
	glBindTexture(GL_TEXTURE_2D, texture_id);
	if(tgainfo->bpp > 24){
		
		gluBuild2DMipmaps(GL_TEXTURE_2D,  4, tgainfo->width, tgainfo->height, GL_RGBA, GL_UNSIGNED_BYTE, tgainfo->data);
		std::cout<< " alpha texture " << std::endl;
	}
	else{
		gluBuild2DMipmaps(GL_TEXTURE_2D,  3, tgainfo->width, tgainfo->height, GL_RGB, GL_UNSIGNED_BYTE, tgainfo->data); //upload the texture and create their mipmaps
		std::cout<< "no alpha texture " << std::endl;
	}
	std::cout << "LoadTGA :: Texture Binded" << std::endl << " LoadTGA :: Textures Parameters" << std::endl;
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); //set the mag filter
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	//set the min filter
	std::cout << "LoadTGA :: Parameters done, returning" << std::endl;
	return true;
}

void Textures::ActivateTexture()
{
	glEnable( GL_TEXTURE_2D );
	glBindTexture(GL_TEXTURE_2D, texture_id);
}
        